
/*

Script: Tree.js
	Create folder trees.

Copyright:
	Copyright (c) 2007-2008 Greg Houston, <http://greghoustondesign.com/>.	

License:
	MIT-style license.	

*/
function buildTree(treeID) {
	var iconPath = MUI.path.plugins + "tree/images/";
	$$("#" + treeID + " li.folder").each(function (folder) {
		var folderContents = folder.getChildren("ul");
		var folderImage = new Element("img", {"src":iconPath + "_open.gif", "width":18, "height":18}).inject(folder, "top");

		// Determine which open and close graphic each folder gets
		if (folder.hasClass("root")) {
			folder.minus = iconPath + "Rminus.gif";
			folder.plus = iconPath + "Rplus.gif";
		} else {
			if (folder.hasClass("first")) {
				folder.minus = iconPath + "Fminus.gif";
				folder.plus = iconPath + "Fplus.gif";
			} else {
				if (folder.getNext()) {
					folder.minus = iconPath + "Tminus.gif";
					folder.plus = iconPath + "Tplus.gif";
				} else {
					folder.minus = iconPath + "Lminus.gif";
					folder.plus = iconPath + "Lplus.gif";
				}
			}
		}
		var image = new Element("img", {"src":folder.minus, "width":18, "height":18}).addEvent("click", function () {
			if (folder.hasClass("f-open")) {
				image.setProperty("src", folder.plus);
				folderImage.setProperty("src", iconPath + "_closed.gif");
				folderContents.each(function (el) {
					el.setStyle("display", "none");
				});
				folder.removeClass("f-open");
			} else {
				image.setProperty("src", folder.minus);
				folderImage.setProperty("src", iconPath + "_open.gif");
				folderContents.each(function (el) {
					el.setStyle("display", "block");
				});
				folder.addClass("f-open");
			}
		}).inject(folder, "top");
		if (!folder.hasClass("f-open")) {
			image.setProperty("src", folder.plus);
			folderContents.each(function (el) {
				el.setStyle("display", "none");
			});
			folder.removeClass("f-open");
		}

		// Add connecting branches to each file node
		folderContents.each(function (element) {
			var docs = element.getChildren("li.doc");
			docs.each(function (el) {
				if (el == docs.getLast() && !el.getNext()) {
					new Element("img", {"src":iconPath + "L.gif", "width":18, "height":18}).inject(el.getElement("span"), "before");
				} else {
					new Element("img", {"src":iconPath + "T.gif", "width":18, "height":18}).inject(el.getElement("span"), "before");
				}
			});
		});
	});
	
	// Add connecting branches to each node
	$$("#" + treeID + " li").each(function (node) {
		node.getParents("li").each(function (parent) {
			if (parent.getNext()) {
				new Element("img", {"src":iconPath + "I.gif", "width":18, "height":18}).inject(node, "top");
			} else {
				new Element("img", {"src":iconPath + "spacer.gif", "width":18, "height":18}).inject(node, "top");
			}
		});
	});
	$$("#" + treeID + " li.doc").each(function (el) {
		new Element("img", {"src":iconPath + "_doc.gif", "width":18, "height":18}).inject(el.getElement("span"), "before");
	});
}
function p(e) {
	var d = new Element("div").grab(e);
	alert(d.get("html"));
}
function loadTree(treeid, dataURL, dataURLParams, event) {
	try {
		var request = new Request.JSON({url:dataURL, onComplete:function (json, text) {
			if (json.error.errorcode == 0) {
				var root = $(treeid);
				json.recorders.each(function (item, index) {
					//树节点的数据从基本树形表中读取
					//或是从其他表（扩展节点下的）读取
					//同时增加item.pcol, item.pcolvalue , item.vcol, item.vcolvaule
					//item.datafrom = 1;
					item.pcolvalue = -1 ; 
					if(item.id==243) item.pcolvalue
					treenode = item;
  						//当前节点ID
					var liId = treeid + "_li_" + treenode.id;
					  	//当前节点的父节点ID
					var pulId = treeid + "_ul_" + treenode.pid;
						  //创建节点NODE
					var node = new Element("li", {"id":liId, "class":"doc"});
					var span = new Element("span");
					
					//在function外改变节点显示
					//if (nodeUI != null && nodeUI != undefined) {
					 
					//	eval(event.onAddElement + "(node,item)");
						//eval("changeNodeUI(node,item)");
					//}
					var nodetext = new Element("a", {"text":treenode.title});
					span.grab(nodetext);
					node.grab(span);
					if (event != null && event != undefined) {
						if (event.onNodeClick) {
							nodetext.addEvent("click", function (e) {
								eval(event.onNodeClick + "(item)");
								return false;
							});
						}
						if (event.onAddElement) {
							eval(event.onAddElement + "(span,item)");
						}
					}	  
						  //alert(treenode.havecid);
						  //如果这个节点有子节点
					if (treenode.havecid > 0) {
						node.set("class", "folder f-open");
						var ulId = treeid + "_ul_" + treenode.id;
						node.grab(new Element("ul", {"id":ulId}));
					}
					if (treenode.pid == -1) {
						node.set("class", "folder f-open first");
						root.grab(node);
					} else {
						root.getElementById(pulId).grab(node);
					}
				});
				buildTree(treeid);
			} else {
				MUI.notification("<font color=red>\u9519\u8bef\uff1a</font>(" + json.error.retstring + ")");
			}
		}}).get(dataURLParams);
	}
	catch (ex) {
		MUI.notification("<font color=red>\u9519\u8bef\uff1a</font>\u8bbf\u95ee\u5931\u8d25(" + ex.message + ")");
	}
}

